import { getEntireRoomList } from '@/services/modules/entire'
import * as actionType from './constants'

export const changeCurrentPageAction = (currentPage) => ({
  type: actionType.CHANGE_CURRENT_PAGE,
  currentPage
})

export const changeRoomListAction = (roomList) => ({
  type: actionType.CHANGE_ROOM_LIST,
  roomList
})

export const changeTotalCountAction = (totalCount) => ({
  type: actionType.CHANGE_TOTAL_COUNT,
  totalCount
})

export const changeIsLoadingAction = (isLoading) => ({
  type: actionType.CHANGE_IS_LOADING,
  isLoading
})

export const fetchRoomListAction = (page = 0) => {

  return async (dispatch,getState) => {

    // 根据页码获取最新的数据
    // const currentPage = getState().entire.currentPage
    dispatch(changeCurrentPageAction(page))
    
    // 请求前遮罩层显示
    dispatch(changeIsLoadingAction(true))
    const res = await getEntireRoomList(page*20)
    dispatch(changeIsLoadingAction(false))
    // 请求前遮罩层隐藏
    const roomLists = res.list
    const totalCounts = res.totalCount

    
    dispatch(changeRoomListAction(roomLists))
    dispatch(changeTotalCountAction(totalCounts))
    
    
  }
}